<?php
// 设置允许其他域名访问
header('Access-Control-Allow-Origin:*');
// 设置允许的响应类型 
header('Access-Control-Allow-Methods:POST, GET');
// 设置允许的响应头 
header('Access-Control-Allow-Headers:x-requested-with,content-type');
// 响应头表示是否可以将对请求的响应暴露给页面
header('Access-Control-Allow-Credentials: true');
$json = file_get_contents("php://input");
$arr = json_decode($json,true);
include "config.php";
$curPage = $arr["page"];
$seachVal = $arr["seachVal"];
$dataType = $arr["dataType"];
if($seachVal){
    $result = $redis->keys("*".$seachVal."*");
}else{
    $result = $redis->keys("*");
}
$arr = array();
foreach($result as $key){
    $row = array();
    $type = $redis->type($key);
    $value = "";
    // //string / int(1)  //set /int(2)  //list / int(3)   //zset / int(4)   //hash /int(5)
    if($type === 1 && $dataType === "string"){  // 只取出string类型
        $value = $redis->get($key);
    }elseif($type === 2  && $dataType === "set"){
        $value = $redis->smembers($key);
    }elseif($type === 3 && $dataType === "list"){
        $listNum = $redis->llen($key);
        $value = $redis->lrange($key,0,$listNum);
    }elseif($type === 4 && $dataType === "zset"){
        $value = array();
        $num = $redis->zcard($key);  // 成员数量
        $result = $redis->zrange($key,0,$num,WITHSCORES);
        $result_ = array();
        foreach($result as $k=>$v){
            $value[$v] = $k; 
        }
    }elseif($type === 5 && $dataType === "hash"){
        $value = $redis->hgetall($key);
    }
    if($value){
        $row["key"] = $key;
        $row["value"] = $value;
        $arr[] = $row;
    }
}
sort($arr);
$startNum = ($curPage-1) * 9;  // 起始索引
$returnArr = array_slice($arr,$startNum,9);  // 截取9个数据
$result = array();
$result["pageNum"] = ceil(count($arr)/9);
$result["data"] = $returnArr;
$result["resultNum"] = count($arr);
echo json_encode($result);